<%--
  Created by IntelliJ IDEA.
  User: leixiaolong
  Date: 2018/11/21
  Time: 5:10 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>机构管理</title>
    <link rel="stylesheet" href="/static/css/rpt-condition.css" media="all" type="text/css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/layui/css/layui.css" media="all"
          type="text/css">

</head>
<body class="layui-bg-gray">
<div style="margin: 10px; background-color: white;">
    <div style="border-color: black; border-style: solid; border-width: 1px;"
         lay-filter="select-condition">
        <div class="layui-form" lay-filter="select-condition">
            <div style="height: 20%; margin-top: 15px;">
                <div class="layui-form-item layui-input-inline">
                    <label class="layui-form-label">角色名称：</label>
                    <input type="text" class="layui-input-inline dom-input" id="role_name"
                           placeholder="支持模糊查询">
                </div>
                <div class="layui-form-item layui-input-inline">
                    <label class="layui-form-label">角色状态：</label>
                    <div class="layui-input-inline">
                        <select class="layui-form-select" id="role_stat">
                            <option value='' selected>全部</option>
                        </select>
                    </div>
                </div>
            </div>
            <div style="float: right; margin-right: 6%; margin-top: -50px;">
                <button class="layui-btn layui-btn-normal layui-btn-radius" lay-submit data-type="search">搜索</button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-radius" data-type="reset"
                        style="margin-right: 20px;">重置
                </button>
            </div>
            <div style="clear:both"></div>
        </div>
    </div>

    <div class="layui-bg-gray">
        <div class="layui-btn-group" style="float: right; margin-right: 100px; margin-top: 5px;">
            <button class="layui-btn layui-btn-sm layui-btn-primary" lay-submit data-type="add">新增</button>
            <button class="layui-btn layui-btn-sm layui-btn-primary">导出</button>
        </div>
        <div style="clear:both"></div>
    </div>

    <table class="layui-hide" id="tab_role" lay-filter="tab-role-filter"></table>
</div>
</body>
</html>

<script type="text/html" id="editBar">
    <a class="layui-btn layui-btn-xs" lay-event="grant">角色授权</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script language='javascript' src="${pageContext.request.contextPath}/plugins/layui/layui.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-select.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-date.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/utils/moment.js"></script>

<script>
    layui.use(["form", "element", "jquery", "layer", "laydate", "table"], function () {
        var form = layui.form, element = layui.element, layer = layui.layer, $ = layui.jquery;
        var laydate = layui.laydate, table = layui.table;

        initCodeTableSelect("role_stat", "/base/getRoleStatList", null);

        //初始化表
        var tableIns = table.render({
            elem: '#tab_role',
            url: '/sys/getRolePager',
            where: {
                roleName: $('#role_name').val(),
                stat: $('#role_stat').val()
            },
            cellMinWidth: 90,
            height: 'full-230',
            cols: [[
                {field: 'seqNo', title: '序号', type: 'numbers', align: 'center'},
                {field: 'roleId', title: '角色id', align: 'center'},
                {field: 'roleName', title: '角色名称', align: 'center'},
                // {field: 'fatherOrganCode', title: '父级机构id', align: 'center', hide: true},
                // {field: 'fatherOrganName', title: '父级机构名称', align: 'center'},
                // {field: 'organLevel', title: '机构等级', align: 'center',
                //     templet: function(row){
                //         if(row.organLevelDesc == 'undefined' || null == row.organLevelDesc){
                //             return '-';
                //         }
                //         return row.organLevelDesc;
                //     }
                // },
                {
                    field: 'stat', title: '角色状态', align: 'center',
                    templet: function (row) {
                        switch (row.stat) {
                            case 0:
                                return '正常';
                            case 1:
                                return '停用';
                            default:
                                return row.stat;
                        }
                    }
                },
                {
                    field: 'ctime', title: '录入时间', align: 'center',
                    templet: function (row) {
                        return new moment(row.ctime).format('YYYY-MM-DD HH:mm:ss');
                    }
                },
                {
                    field: 'opertaor', title: '操作', align: 'center', templet: '#editBar', fixed: 'right'
                }
            ]],
            done: function () {
                var myDate = new Date();
            },
            page: {
                limit: 10,
                limits: [10, 20, 50]
            }
        });

        //监听表行工具事件
        table.on('tool(tab-role-filter)', function(obj){
            var rowdata=obj.data;  //获取当前行数据
            var layEvent = obj.event;   //获取事件名称
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if(layEvent == 'edit'){
                layer.open({
                    id: 'edit_form',
                    type: 2,
                    title: ['角色信息修改', 'text-align: center; font-size: 20px;'],
                    area: ['800px', '600px'],
                    offset: 'auto',
                    content: '/sys/role_edit_mger',
                    success: function(){
                        var iframeId = document.getElementById('edit_form').getElementsByTagName('iframe')[0].id;
                        $('#'+ iframeId)[0].contentWindow.init(rowdata);
                    }
                });
            }else if(layEvent == 'del'){

                layer.confirm("请确认是否停用", {icon: 3, title:'提示'}, function(index){
                    $.ajax({
                        type: 'POST',
                        url: 'delRoleInfo',
                        data: {id: rowdata.roleId},
                        success: function(data){
                            renderTable('tab_role');
                            layer.close(index);
                        },
                        error: function(data){
                            layer.msg("角色删除失败," + data);
                            layer.close(index);
                        }
                    });
                });

            }else if(layEvent == 'grant'){
                var treeindex = layer.open({
                    id: "role_list",
                    type: 2,
                    title: "请选择角色权限",
                    area: ["500", "80%"],
                    content: '/sys/role_grant',
                    btn: ['确认提交'],
                    success: function(){
                        var iframeId = document.getElementById('role_list').getElementsByTagName('iframe')[0].id;
                        $('#'+ iframeId)[0].contentWindow.init(rowdata.roleId);
                    },
                    yes: function(){
                        var iframeId = document.getElementById('role_list').getElementsByTagName('iframe')[0].id;
                        var choosedData = $('#'+ iframeId)[0].contentWindow.getChoosed();
                        console.log(choosedData);
                        var arr=[];

                        choosedData.forEach(function(e){
                            var nodeinfo = {
                                menuId: e.nodeId,
                                menuName: e.context,
                                parentId: e.parentId
                            };
                            arr.push(nodeinfo);
                        });

                        var privData = {
                            roleId: obj.data.roleId,
                            roleName: obj.data.roleName,
                            data: arr
                        };
                        //ajax调用后台
                        $.ajax({
                            url: 'resetRolePriveleges',
                            type: 'POST',
                            contentType: 'application/json',
                            data: JSON.stringify(privData),
                            success: function(result){
                                if(result.data == true){
                                    layer.close(treeindex);
                                    layer.msg("角色权限设置成功", {icon: 1});
                                }else{
                                    layer.close(treeindex);
                                    layer.msg("系统繁忙请稍后重试", {icon: 5});
                                }
                            },
                            error: function(result){
                                layer.msg("系统异常,请联系系统管理员.message:"+result, {icon:5});
                            }
                        });

                    }
                });
            }
        });

        var actives = {
            search: function () {
                renderTable('tab_role');
            },
            reset: function () {
                $('#role_name').val('');
            },
            add: function () {
                layer.open({
                    type: 2,
                    title: ['新增角色', 'text-align: center; font-size: 20px;'],
                    area: ['800px', '600px'],
                    offset: 'auto',
                    content: '/sys/role_add_mger'
                });
            }
        };

        form.on('submit', function (data) {
            actives[$(this).data("type")].call(this);
        });

        function renderTable(tableId) {
            table.reload(tableId, {
                where: {
                    roleName: $('#role_name').val(),
                    stat: $('#role_stat').val()
                }
            });
        }
    });
</script>